<template>
  <el-card class="box-card">
    <!-- 搜索区域 -->
    <el-row :gutter="20">
      <el-col :span="8">
        <el-input placeholder="请输入书名" v-model="queryInfo.bookTitle">
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="findAll()"
          ></el-button>
        </el-input>
      </el-col>
      <el-col :span="8">
        <el-input placeholder="请输入借阅人" v-model="queryInfo.userName">
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="findAll()"
          ></el-button>
        </el-input>
      </el-col>
    </el-row>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="borrowId" label="借阅编号" align="center">
      </el-table-column>
      <el-table-column prop="bookTitle" label="书名" align="center">
      </el-table-column>
      <el-table-column prop="userName" label="借阅人" align="center">
      </el-table-column>
      <el-table-column prop="borrowTime" label="借阅时间" align="center">
      </el-table-column>
      <el-table-column prop="checkTime" label="审核时间" align="center">
      </el-table-column>
      <el-table-column prop="status" label="审核状态">
        <template v-slot="scope">
          <span
            >{{
              scope.row.status === 0
                ? '待审核'
                : scope.row.status === 2
                ? '审核不通过'
                : '审核通过'
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="审核" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            :disabled="scope.row.status !== 0"
            @click="changeState(scope.row, 1)"
            >审核通过</el-button
          >
          <el-button
            size="mini"
            type="danger"
            :disabled="scope.row.status !== 0"
            @click="changeState(scope.row, 2)"
            >审核拒绝</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div class="block" style="text-align: center">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.page"
        :page-sizes="[1, 3, 5, 7]"
        :page-size="5"
        layout="total, sizes, prev, pager, next"
        :total="total"
      >
      </el-pagination>
    </div>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      // 条件
      queryInfo: {
        page: 1,
        pageSize: 5,
      },
      total: 0,
      info: {},
    }
  },
  created() {
    this.findAll()
  },
  methods: {
    // 每页显示多少条数据回调
    handleSizeChange(pageSize) {
      this.queryInfo.pageSize = pageSize
      this.findAll()
    },
    // 当前页发生改变的回调
    handleCurrentChange(page) {
      this.findAll(page)
    },
    // 获取当前时间
    getCurrentTime() {
      let yy = new Date().getFullYear()
      let mm =
        new Date().getMonth() + 1 < 10
          ? '0' + (new Date().getMonth() + 1)
          : new Date().getMonth() + 1
      let dd =
        new Date().getDate() < 10
          ? '0' + (new Date().getDate() + 1)
          : new Date().getDate() + 1
      let hh =
        new Date().getHours() < 10
          ? '0' + new Date().getHours()
          : new Date().getHours()
      let mf =
        new Date().getMinutes() < 10
          ? '0' + new Date().getMinutes()
          : new Date().getMinutes()
      let ss =
        new Date().getSeconds() < 10
          ? '0' + new Date().getSeconds()
          : new Date().getSeconds()
      return yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss
    },
    async findAll(page = 1) {
      this.queryInfo.page = page
      const { data: res } = await this.$http.post(
        '/borrow/findAll',
        this.queryInfo
      )
      this.tableData = res.dataList
      this.total = res.count
    },
    async changeState(row, status) {
      await this.$confirm(status == 1 ? '审核通过吗？' : '审核不通过吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          this.info = row
          this.info.status = status
          this.info.checkTime = this.getCurrentTime()

          // // 发请求，保存数据库
          await this.$http
            .put('/borrow/update', this.info)
            .then((res) => {
              if (res.data.code == '200') {
                this.$message.success('审核完成')
              } else {
                this.$message.info('更新失败')
              }
            })
            .finally(() => {
              // 重新获取数据
              this.findAll(this.queryInfo.page)
            })
        })
        .catch(() => {
          this.$message.info('当前操作取消')
        })
    },
  },
}
</script>

<style>
</style>